package com.oodso.sell.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据的格式化校验
 *
 * @author huangzhenwei
 */
public class CheckTextUtil {
    public static final int INPUT_PHONENUMBER = 0;// 电话号吗
    public static final int INPUT_IDCARD = 1;// 身份证号
    public static final int INPUT_USERNAME = 2;// 用户名
    public static final int INPUT_USERID = 3;// 用户账号
    public static final int INPUT_VERIFYCODE = 4;// 校验码
    public static final int INPUT_MONEY = 5;// 金额
    public static final int INPUT_BANK_CARD = 6;// 银行卡号
    public static final int INPUT_PASSWORD = 7;// 密码
    public static final int INPUT_EMAIL = 8;// 邮箱
    public static final int INPUT_NAME = 9;// 姓名
    public static final int INPUT_ENDEARMENT = 10;// 昵称
    public static final int INPUT_PREINFO = 11;// 预留信息
    public static final int INPUT_AMOUNT = 12;// 金额
    public static final int INPUT_VERIFYPSW = 13;// 验证码
    public static final int INPUT_PASSWORD_2 = 14;// 密码


    /**
     * 检查输入框合法性(参数: 上下文,输入字符串,类型) 合法返回true 否则false
     */
    public static boolean CheckKindsInput(Context context, EditText text, int type) {
        String str = text.getText().toString();
        boolean ret = false;
        switch (type) {
            case INPUT_PHONENUMBER: {
                boolean isValid = CheckPhoneNumber(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "请输入手机号码！";
                    } else {
                        msg = "手机号格式不正确，请重新输入！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_IDCARD: {
                boolean isValid = CheckIdCard(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "身份证号码不能为空";
                    } else {
                        msg = "身份证号码格式不对";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }

            break;
            case INPUT_USERNAME: {
                boolean isValid = CheckUserName(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "请输入真实姓名！";
                    } else {
                        msg = "真实姓名格式不对！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }

            break;
            case INPUT_USERID: {
                boolean isValid = CheckBankCard(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "银行卡号不能为空";
                    } else {
                        msg = "银行卡号格式不对";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_VERIFYCODE: {
                String msg = "";
                if (str == null || str.equals("")) {
                    msg = "请输入短信动态码！";
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_MONEY: {
                boolean isValid = CheckMoney(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "请输入金额！";
                    } else {
                        msg = "金额不正确，最多保留小数点后两位！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_BANK_CARD: {
                boolean isValid_phone = CheckPhoneNumber(str);
                String msg = "";
                if (isValid_phone) {
                    ret = true;
                } else {
                    msg = "请输入正确的手机号码或银行账号";
                    showDialog(context, msg);
                }
            }
            break;
            case INPUT_PASSWORD: {
                boolean isValid = CheckPassword(str);
                if (!isValid) {
                    String msg = "";
                    if (str == null || str.equals("")) {
                        msg = "请输入电子账户登录密码！";
                    } else {
                        msg = "密码格式不正确，请重新输入！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_PASSWORD_2: {
                boolean isValid = CheckPassword2(str);
                if (!isValid) {
                    String msg = "";
                    if (str == null || str.equals("")) {
                        msg = "请输入取款密码！";
                    } else {
                        msg = "取款密码不正确，请重新输入！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_EMAIL: {
                boolean isValid = CheckEmail(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "请输入电子邮箱！";
                    } else {
                        msg = "邮箱格式不正确，请重新输入！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_NAME: {
                boolean isValid = CheckName(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = "请输入姓名！";
                    } else {
                        msg = "姓名格式不正确，请重新输入！";
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_ENDEARMENT: {
                int isValid = CheckEndearment(str);
                String msg = "";
                if (isValid != 0) {
                    if (str == null || str.equals("")) {
                        msg = "请输入昵称！";
                    } else {
                        if (isValid == 1) {
                            msg = "昵称不能为纯数字！";
                        } else {
                            msg = "昵称格式不正确，请重新输入！";
                        }
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_PREINFO: {
                if (str == null || str.equals("")) {
                    showDialog(context, "请输入预留信息！");
                } else {
                    boolean isValid = CheckPreInfo(str);
                    if (!isValid) {
                        showDialog(context, "预留信息格式不正确，请重新输入！");
                    } else {
                        ret = true;
                    }
                }
            }
            break;
            case INPUT_AMOUNT:
                if (str == null || str.equals("")) {
                    showDialog(context, "请输入金额！");
                } else {
                    float amount = Float.valueOf(str);
                    if (amount < 1) {
                        showDialog(context, "充值金额最少为1.00元！");
                    } else if (amount > 50000) {// 账户余额
                        showDialog(context, "充值金额大于账户余额，请重新输入！");
                    } else {
                        ret = true;
                    }
                }
                break;
            default:
                ret = true;
                break;
        }

        if (!ret) {
            text.requestFocus();
        }
        return ret;
    }


    /**
     * 检查输入框合法性(参数: 上下文,输入字符串,类型,错误信息) 合法返回true 否则false
     */
    public static boolean CheckKindsInput(Context context, EditText text, int type, String err) {
        String str = text.getText().toString();
        boolean ret = false;
        switch (type) {
            case INPUT_PHONENUMBER: {
                boolean isValid = CheckPhoneNumber(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_IDCARD: {
                boolean isValid = CheckIdCard(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }

            break;
            case INPUT_USERNAME: {
                boolean isValid = CheckUserName(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }

            break;
            case INPUT_USERID: {
                boolean isValid = CheckBankCard(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s必须为14-30位", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_VERIFYCODE: {
                String msg = "";
                if (str == null || str.equals("")) {
                    msg = String.format("请输入%s！", err);
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_MONEY: {
                boolean isValid = CheckMoney(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_BANK_CARD: {
                boolean isValid_card = CheckBankCard(str);
                String msg = "";
                if (isValid_card) {
                    msg = String.format("请输入%s！", err);
                } else {
                    msg = err;
                    showDialog(context, msg);
                }
            }
            break;
            case INPUT_PASSWORD: {
                boolean isValid = CheckPassword(str);
                if (!isValid) {
                    String msg = "";
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_PASSWORD_2: {
                boolean isValid = CheckPassword2(str);
                if (!isValid) {
                    String msg = "";
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_EMAIL: {
                boolean isValid = CheckEmail(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_NAME: {
                boolean isValid = CheckName(str);
                String msg = "";
                if (!isValid) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        msg = String.format("%s格式不正确，请重新输入！", err);
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_ENDEARMENT: {
                int isValid = CheckEndearment(str);
                String msg = "";
                if (isValid != 0) {
                    if (str == null || str.equals("")) {
                        msg = String.format("请输入%s！", err);
                    } else {
                        if (isValid == 1) {
                            msg = String.format("%s不能为纯数字！", err);
                        } else {
                            msg = String.format("%s格式不正确，请重新输入！", err);
                        }
                    }
                    showDialog(context, msg);
                } else {
                    ret = true;
                }
            }
            break;
            case INPUT_PREINFO: {
                if (str == null || str.equals("")) {
                    showDialog(context, String.format("请输入%s！", err));
                } else {
                    boolean isValid = CheckPreInfo(str);
                    if (!isValid) {
                        showDialog(context, String
                                .format("%s格式不正确，请重新输入！", err));
                    } else {
                        ret = true;
                    }
                }
            }
            break;
            default:
                ret = true;
                break;
        }

        if (!ret) {
            text.requestFocus();
        }
        return ret;
    }


    public static void showDialog(Context context, String string) {

        // new AlertDialog.Builder(context).setMessage(string)
        // .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        // @Override
        // public void onClick(DialogInterface dialog, int id) {
        //
        // }
        // }).create().show();
        Toast.makeText(context, string, Toast.LENGTH_SHORT).show();
    }


    /**
     * 检查用户名合法性(参数: 用户名) 手机号/邮箱/昵称
     */
    public static boolean CheckUserName(String str) {
        boolean ret = false;

        int len = str.length();
        if ((len == 0) || ("".equals(str))) {
            return ret;
        }
        Pattern p = Pattern
                .compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
        Matcher m = p.matcher(str);
        ret = m.matches();
        if (ret) {
            return ret;
        } else {
            p = Pattern
                    .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
            m = p.matcher(str);
            ret = m.matches();
            if (ret) {
                return ret;
            } else {
                p = Pattern.compile("^([0-9]*)$");
                m = p.matcher(str);
                ret = m.matches();
                if (ret) {
                    return false;
                } else {
                    p = Pattern.compile("^(?![^a-zA-Z0-9]+$).{6,20}$");
                    m = p.matcher(str);
                    ret = m.matches();
                }
            }
        }

        return ret;
    }


    /**
     * 检查手机号码合法性(参数: 手机号码)
     */
    public static boolean CheckPhoneNumber(String str) {
        if (!TextUtils.isEmpty(str)) {
            int len = str.length();
            if (len != 11) {
                return false;
            } else {
                Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
                Matcher m = p.matcher(str);
                return m.matches();
            }
        } else {
            return false;
        }
    }

    /**
     * 检查手机号码合法性(参数: 手机号码/电话号码)
     */
    public static boolean CheckPhone(String str) {
        if (!TextUtils.isEmpty(str)) {
            Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9])|(17[0-9]))\\d{8}$");
            Matcher m = p.matcher(str);
            boolean b = m.matches();
            Pattern p2 = Pattern.compile("^(010|02\\d|0[3-9]\\d{2})?\\d{6,8}$");
            Matcher m2 = p2.matcher(str);
            boolean b2 = m2.matches();
            return b || b2;
        } else {
            return false;
        }
    }

    /**
     * 判定输入汉字
     *
     * @param c 输入汉字
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    /**
     * 检测String是否全是中文
     *
     * @param name 输入汉字
     * @return
     */
    public static boolean checkNameChese(String name) {
        boolean res = true;
        char[] cTemp = name.toCharArray();
        for (int i = 0; i < name.length(); i++) {
            if (!isChinese(cTemp[i])) {
                res = false;
                break;
            }
        }
        return res;
    }

    /**
     * 检查电子邮箱合法性(参数: 电子邮箱)
     */
    public static boolean CheckEmail(String str) {
        Pattern p = Pattern
                .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 检查姓名合法性(参数: 姓名)
     */
    public static boolean CheckName(String str) {
        Pattern p = Pattern.compile("^([\u4e00-\u9fa5]+|([a-zA-Z]+\\s?)+)$");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static int CheckEndearment(String str) {
        int ret = 0;
        Pattern p = Pattern.compile("^([0-9]*)$");
        Matcher m = p.matcher(str);
        if (m.matches()) {
            ret = 1;
        } else {
            p = Pattern.compile("^(?![^a-zA-Z0-9]+$).{4,20}$");
            m = p.matcher(str);
            if (!m.matches()) {
                ret = 2;
            }
        }
        return ret;
    }
    /**
     * 店铺名称
     * @param str
     * @return
     */
    public static String CheckStop(String str) {
        return str.replaceAll("[^a-zA-Z0-9\u4E00-\u9FA5]","").trim();
    }
    /**
     * 英文字母数字
     * @param str
     * @return
     */
    public static String Checkyyzz(String str) {
        return str.replaceAll("[^a-zA-Z0-9]","").trim();
    }
    /**
     * 检查金额合法性(参数: 金额)
     */
    public static boolean CheckMoney(String str) {
        Pattern p = Pattern.compile("^(([0-9]\\d{0,12}))(\\.\\d{1,2})?$");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 检查身份证号码合法性(参数: 身份证号) 身份证号码可能有15和18位两种
     */
    public static boolean CheckIdCard(String str) {

        Pattern patternSfzhm2 = Pattern
                .compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$");
        Matcher matcherSfzhm2 = patternSfzhm2.matcher(str);

        Pattern patternSfzhm1 = Pattern
                .compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)$");
        Matcher matcherSfzhm1 = patternSfzhm1.matcher(str);

        if (matcherSfzhm1.find() || matcherSfzhm2.find()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 检查银行卡号码合法性(参数: 银行卡号) 银行卡号码可能有14-20位 暂时是14位以上的（包括14位）
     */
    public static boolean CheckBankCard(String str) {
        int len = str.length();
        if ((len == 0) || ("".equals(str))) {
            return false;
        }
        if ((len > 0 && len < 14) || (len > 30)) {
            return false;
        }
        /*
		 * if(len>16 || len>20) return false; else { for(int i=0;i<len;i++){
		 * char temp = str.charAt(i); //if() } }
		 */
        return true;
    }


    /**
     * 检查密码合法性(参数: 密码) 暂时是6位以上的（包括6位）
     */
    public static boolean CheckPassword(String str) {
        int len = str.length();
        if ((len == 0) || ("".equals(str))) {
            return false;
        }
        Pattern p = Pattern.compile("^(?![^a-zA-Z]+$)(?!\\D+$).{6,20}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    public static boolean CheckPassword2(String str) {
        int len = str.length();
        if ((len == 0) || ("".equals(str))) {
            return false;
        }
        Pattern p = Pattern.compile("^(?!$).{6,20}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }


    /**
     * 检查预留信息合法性(参数: 预留信息)
     */
    public static boolean CheckPreInfo(String str) {
        // Pattern p = Pattern
        // .compile("^([\u4e00-\u9fa5]+|([a-zA-Z]+\\s?)+)$");
        // Matcher m = p.matcher(str);
        // return m.matches();
        return true;
    }


    public static boolean checkRealName(EditText u, Context context) {
        // true为全部是汉字，否则是false
        Pattern pa = Pattern.compile("^[\u4e00-\u9fa5]*$");
        Matcher matcher = pa.matcher(u.getText().toString());
        return matcher.find();
    }

    /**
     * 验证两次输入密码是否一致
     *
     * @param pwd        密码
     * @param confirmPwd 确认密码
     * @return
     */
    public static boolean isPwdEqualsConfirmPwd(String pwd, String confirmPwd) {
        if (pwd.equals(confirmPwd)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断字符串是否为纯数字
     *
     * @param str 字符串
     * @return
     */
    public static boolean isNumeric(String str) {
        return str.matches("[0-9]+");
    }

    /**
     * 判断字符串是否包含特殊字符
     * @param str
     * @return
     */
    public static boolean isHasSpecailChars(String str){
        return str.matches("^[A-Za-z0-9\u4e00-\u9fa5,.!;?:，。；：！——？……()（）【】<>《》~、\\s//[//]]+$");
    }
}
